import time
import random
import socketio

# 创建一个Socket.IO客户端
sio = socketio.Client()


@sio.event
def connect():
    print('Connection established')


@sio.event
def disconnect():
    print('Disconnected from server')


@sio.event
def message(data):
    print('Received message:', data)

# 运行 python testws.py
# 连接到 Flask-SocketIO 服务器
sio.connect('http://localhost:5000')

# 每分钟发送一个随机数到服务器
try:
    while True:
        random_number = random.randint(1, 100)
        print(f'Sending random number: {random_number}')
        sio.send(f'Random number: {random_number}')
        time.sleep(1)  # 等待一秒钟
except KeyboardInterrupt:
    sio.disconnect()
